﻿var Index = 0;
var dragState = true;
var userInfo;


$(function() {　
	console.log(typeof(localStorage.userInfo)!="undefined")
	if (typeof(localStorage.userInfo)!="undefined") {
	    userInfo = JSON.parse(localStorage.userInfo); 
	    console.log(localStorage.userInfo);
	    $('#userName .value').val(userInfo.trueName);
	    $('#userMobile .value').val(userInfo.mobile);
	    $('#userID .value').val(userInfo.idCard);
	    $('#userSex .value').val(userInfo.sex);	    
//	    var vm = avalon.define({
//			$id: 'personData',
//			userInfo: userInfo
//		});
	  }
	else{
	//  mui.toast("检测不到登录状态，请重新登录！");
	}
	mui.plusReady(function() {
		//预加载页面
	});
	mui.init({
		gestureConfig: {
			tap: true,
			//默认为true
			doubletap: true,
			//默认为false
			longtap: true,
			//默认为false
			swipe: true,
			//默认为true
			drag: true,
			//默认为true
			hold: false,
			//默认为false，不监听
			release: false
		},
		beforeback: function(){
			//获得列表界面的webview
			var lists = plus.webview.all();
			for (var i in lists) {
				if (lists[i].id == 'personCenter') {
					var personCenter = plus.webview.getWebviewById('personCenter');
					mui.fire(index,'backPerson',{jsonStr:"true"});
					return
				}				
			}
			for (var i in lists) {
				if (lists[i].id == 'index') {
					var index = plus.webview.getWebviewById('index');
					mui.fire(index,'backIndex',{jsonStr:"true"});
					return
				}			
			}
			
//			App.showPage('index');
		}
	});
		
		
//		mui.back = function() {
//  		App.showPage('index')
//  	};	
	
	$('#secondNav .nav-btn').on('tap',function(){
		$('#secondNav .nav-btn').removeClass('mui-active');
		$('#secondNav .nav-btn').eq($(this).index()).addClass('mui-active');
		$('.info-row').removeClass('mui-active');
		$('.info-row').eq($(this).index()).addClass('mui-active');
	});
	
	$('#submitBtn').on('tap',function(){
		if (typeof(localStorage.userInfo)!="undefined") {
		    userInfo = JSON.parse(localStorage.userInfo); 
		    console.log(localStorage.userInfo)
		 }
		console.log( JSON.stringify(userInfo))
		if ($('#userName .value').val() == '') {
			mui.toast('用户名不能为空！')
		}
		else{
			if ($('#userMobile .value').val() == '') {
				mui.toast('手机号不能为空！');				
			}
			else{
				if ($('#userSex .value').val() == '') {
					$('#userSex .value').val('男');
				}
				else{
					//暂不检查
					var params = {
						sysChildrenId : userInfo.sysChildrenId,
						mobile : $('#userMobile .value').val(),
						trueName : $('#userName .value').val(),
						sex :    $('#userSex .value').val(),
						idCard : $('#userID .value').val(),
						csrq :   '',
						cityCode : '1',
						areaCode : '2',
						townCode : '3',
						streetCode : '4',
						buildingCode : '5',
						roomCode: '6',
						communityCode: '7'
					}
					console.log(JSON.stringify(params))
					App.personInfo(params,function(data){
						console.log(JSON.stringify(data));
						mui.toast(data.msg);
						if(data.result){
							var lists = plus.webview.all();
							for (var i in lists) {
								if (lists[i].id == 'personCenter' ) {
	//								mui.back();
									App.showPage('personCenter');
								}
								else if (lists[i].id == 'login' ) {
									App.showPage('index');
								}
							}
								mui.back();
						}
					});
				}
			}
		}
	});
	
	$('#sureBtn').on('tap',function(){
			
				if (typeof(localStorage.userInfo)!="undefined") {
					userInfo = JSON.parse( localStorage.userInfo );
					if ($('#oldPassword').val() != userInfo.password) {
						mui.toast('原密码不正确！');
					}
					else{
						if ($('#password').val() != $('#RePassword').val()) {
							mui.toast('两次密码输入不相同！');
						}
						else{
							var params = {
								mobile : userInfo.mobile,
								password : $('#oldPassword').val(),
								newPassword : $('#password').val()
							}
							App.appModifyPassword(params,function(data){
								console.log(JSON.stringify(data));
								mui.toast(data.msg);
							});
						}
					}
				}
				else{
					mui.toast('未检测到登录状态，请重新登录');
				}
	});
	
	

	var timer;
	document.querySelector('body').addEventListener('drag', function(event) {
		if (dragState) {			
			//往左滑
			if(event.detail.deltaX < -50){
				dragState = false;
				Index = 1;
				$('#secondNav .nav-btn').removeClass('mui-active');
				$('#secondNav .nav-btn').eq(Index).addClass('mui-active');
				$('.info-row').removeClass('mui-active');
				$('.info-row').eq(Index).addClass('mui-active');
			}
			//往右滑
			else if(event.detail.deltaX > 50){
				dragState = false;
				Index = 0;
				$('#secondNav .nav-btn').removeClass('mui-active');
				$('#secondNav .nav-btn').eq(Index).addClass('mui-active');
				$('.info-row').removeClass('mui-active');
				$('.info-row').eq(Index).addClass('mui-active');
			}
			timer = setTimeout(function(){
				dragState = true;
			},300);
		}
		
	});
	
			
			var city_html;
			var cityCode;
			var areaCode;
			$("#prov_select").change(function () {//绑定选择省份触发
                var prov_id = $(this).val(),
                    city_html = '<option value="">请选择...</option>',
                    citys = [];
                citys = addr_arr[prov_id];
                try {
                    for (var i = 0; i < citys.length; i++) {
                        city_html += '<option value="' + citys[i][0] + '">' + citys[i][1] + '</option>';
                    }
                }
                catch (e) { }
                $("#city_select").html(city_html);
                $("#region_select").html('<option value="">请选择...</option>');
            });

            $("#city_select").change(function () {//绑定选择城市触发
            	$('#region,#town,#stree').hide();
                var city_id = $(this).val(),
                    region_html = '<option value="">请选择...</option>',
                    areas = [];
                    console.log(city_id);
                    cityCode = city_id;
                var paramsRegion = {
                	cityCode : city_id
                }
                App.areaList(paramsRegion,function(data){
                	console.log(JSON.stringify(data));
                	areas = data.data;
	                for (var i = 0; i < areas.length; i++) {
	                    region_html += '<option value="' + areas[i].areaCode + '">' + areas[i].areaName + '</option>';
	                }
	                $('#region').css('display','inline-block');
	                $("#region_select").html(region_html);
                });
            });
            
            $("#region_select").change(function () {//绑定选择城市触发
            	$('#town,#stree').hide();
                var town_id = $(this).val(),
                    town_html = '<option value="">请选择...</option>',
                    areas = [];
                    areaCode = town_id;
                    console.log(town_id)
                var paramsRegion = {
                	cityCode : cityCode,
                	areaCode : town_id
                }
                App.townList(paramsRegion,function(data){
                	console.log(JSON.stringify(data));
                	areas = data.data;
	                for (var i = 0; i < areas.length; i++) {
	                    town_html += '<option value="' + areas[i].townCode + '">' + areas[i].townName + '</option>';
	                }
	                $('#town').css('display','inline-block');
	                $("#town_select").html(town_html);
                });
            });
            
            $("#town_select").change(function () {//绑定选择城市触发
            	$('#stree').hide();
                var town_id = $(this).val(),
                    town_html = '<option value="">请选择...</option>',
                    areas = [];
                    townCode = town_id;
                    console.log(townCode)
                var paramsRegion = {
                	cityCode : cityCode,
                	areaCode : areaCode,
                	townCode : townCode
                }
                var town_html = '';
                App.streetList(paramsRegion,function(data){
                	console.log(JSON.stringify(data));
                	areas = data.data;
	                for (var i = 0; i < areas.length; i++) {
	                    town_html += '<option value="' + areas[i].streetCode + '">' + areas[i].streetName + '</option>';
	                }
	                $('#stree').css('display','inline-block');
	                $("#stree_select").html(town_html);
                });
            });
            
            $("#stree_select").change(function () {//绑定选择城市触发
                var town_id = $(this).val(),
                    buliding_html = '<option value="">请选择...</option>',
                    areas = [];
                    townCode = town_id;
                    console.log(townCode)
                var paramsRegion = {
                	cityCode : cityCode,
                	areaCode : areaCode,
                	townCode : townCode
                }
                App.bulidingList(paramsRegion,function(data){
                	console.log(JSON.stringify(data));
                	areas = data.data;
	                for (var i = 0; i < areas.length; i++) {
	                    buliding_html += '<option value="' + areas[i].bulidingCode + '">' + areas[i].bulidingName + '</option>';
	                }
	                $('#buliding').css('display','inline-block');
	                $("#buliding_select").html(buliding_html);
                });
            });

            function getProvIdByName(prov_name) {//工具函数，通过省份名省获取到省份id
                var provinces = [];
                provinces = addr_arr[0];
                prov_name = $.trim(prov_name);
                for (var i = 0; i < provinces.length; i++) {
                    if (prov_name == provinces[i][1]) {
                        return provinces[i][0];
                    }
                }
                return -1;
            }
            function getProvIdByCityId(city_id) {//工具函数，通过城市id获取到省份id
                for (var i = 1; i < 36; i++) {
                    for (var j = 0; j < addr_arr[i].length; j++) {
                        if (addr_arr[i][j][0] == city_id) {
                            return i;
                        }
                    }
                }
                return -1;
            }
            
            function addrInit(prov_id, city_id, area_id) {//工具函数，通过固定的省份id,城市id,地区id来初始化
                var prov_html = "",
                    provinces = [];
                provinces = addr_arr[0];
                for (var i = 0; i < provinces.length; i++) {
                    if (provinces[i][0] != prov_id) {
                        prov_html += '<option value="' + provinces[i][0] + '">' + provinces[i][1] + '</option>';
                    } else {
                        prov_html += '<option value="' + provinces[i][0] + '" selected="selected">' + provinces[i][1] + '</option>';
                    }
                }
                $("#city_select").html(city_select);
                $("#city_select").trigger("change");
//              $("#city_select").find('option[value="' + city_id + '"]').attr("selected", true).trigger("change");
                $("#region_select").find('option[value="' + area_id + '"]').attr("selected", true);
            }
            
            function addrEmptyInit() {//什么都没选，空初始化
                var city_html = '<option value="">请选择...</option>',
                    citys = [];
                var paramsCity = {
                	cityId : 1
                }
                App.cityList(paramsCity,function(data){
                	console.log(JSON.stringify(data));
                	citys = data.data;
	                for (var i = 0; i < citys.length; i++) {
	                    city_html += '<option value="' + citys[i].cityCode + '">' + citys[i].cityName + '</option>';
	                }
	                console.log(city_html)
	                $("#city_select").html(city_html);
                });
            }
//          addrEmptyInit();
            //空启动
            //the end
            
            $('#city_select').on('tap',function(){
            	console.log( $("#city_select").html());
            });
	
});